<?php
/*! tncode 1.2 author:weiyingbin email:277612909@qq.com
//@ object webiste: http://www.39gs.com/archive/259.html
//@ https://github.com/binwind8/tncode
*/
class TnCode
{
    var $im = null;
    var $im_fullbg = null;
    var $im_bg = null;
    var $im_slide = null;
    public static $bg_width = 243;//用户可见的图片宽度
    public static  $bg_height = 58;//用户可见的图片高度
    public static  $mark_width = 17;
    public static   $mark_height = 17;
    var $bg_num = 1;
    var $_x = 0;
    var $_y = 0;
    //容错象素 越大体验越好，越小破解难道越高
    var $_fault = 3;
    function __construct(){
    }
    function make(){
        $this->_init();
        $this->_createSlide();
        $this->_createBg();
        $this->_merge();
        $this->_imgout();
        $this->_destroy();
        return $this->_x;
    }

    //根据时间生成一个随机的x坐标
    public static function createRand($time=''){
        if(!$time) $time = r('time');
        $max = self::$bg_width - self::$mark_width * 2 - 1;
        $str = md5(CODE_KEY1.$time.CODE_KEY2);
        $sum = getAscSum($str);
        $rand = floor(fmod($sum,$max) + self::$mark_width);
        return array($rand,$time);
    }

    function check($offset=''){
        if(!$offset){
            $offset = $_REQUEST['tn_r'];
        }

        $rand = self::createRand();
        return abs($rand[0]-$offset)<=$this->_fault;
    }

    private function _init(){
        $bg = mt_rand(1,$this->bg_num);
        $file_bg = dirname(__FILE__).'/bg/'.$bg.'.png';
        $this->im_fullbg = imagecreatefrompng($file_bg);
        $this->im_bg = imagecreatetruecolor(self::$bg_width, self::$bg_height);
        imagecopy($this->im_bg,$this->im_fullbg,0,0,0,0,self::$bg_width, self::$bg_height);
        $this->im_slide = imagecreatetruecolor(self::$mark_width, self::$bg_height);
        $rand = self::createRand();
        $this->_x = $rand[0];
        $_REQUEST['tn_c'] = 0;
        $this->_y = mt_rand(0,self::$bg_height-self::$mark_height-1);
    }

    private function _destroy(){
        imagedestroy($this->im);
        imagedestroy($this->im_fullbg);
        imagedestroy($this->im_bg);
        imagedestroy($this->im_slide);
    }

    private function _imgout(){
        if(!$_GET['nowebp']&&function_exists('imagewebp')){//优先webp格式，超高压缩率
            $type = 'webp';
            $quality = 40;//图片质量 0-100
        }else{
            $type = 'png';
            $quality = 7;//图片质量 0-9
        }
        header('Content-Type: image/'.$type);
        $func = "image".$type;
        $func($this->im,null,$quality);
    }

    private function _merge(){
        $this->im = imagecreatetruecolor(self::$bg_width, self::$bg_height*3);
        imagecopy($this->im, $this->im_bg,0, 0 , 0, 0, self::$bg_width, self::$bg_height);
        imagecopy($this->im, $this->im_slide,0, self::$bg_height , 0, 0, self::$mark_width, self::$bg_height);
        imagecopy($this->im, $this->im_fullbg,0, self::$bg_height*2 , 0, 0, self::$bg_width, self::$bg_height);

        //@todo remove
        if(IS_DEBUG && r('yiyou')==1){
            $fontFile = ROOT_PATH.'lib/tncode/font/AGENCYB.TTF';
            $black = imagecolorallocate($this->im, 0xff, 0x00, 0x00);
            $rand = self::createRand();
            imagefttext($this->im, 12, 0, 105, 55, $black, $fontFile, 'x='.$this->_x.' rand='.$rand[0]."\r\n\r\nbgHeight=".self::$bg_height."\r\n\r\n".'time='.$rand[1]);
        }
        imagecolortransparent($this->im,0);//16777215
    }

    private function _createBg(){
        $file_mark = dirname(__FILE__).'/img/mark.png';
        $im = imagecreatefrompng($file_mark);
        header('Content-Type: image/png');
        //imagealphablending( $im, true);
        imagecolortransparent($im,0);//16777215
        //imagepng($im);exit;
        imagecopy($this->im_bg, $im, $this->_x, $this->_y  , 0  , 0 , self::$mark_width, self::$mark_height);
        imagedestroy($im);
    }

    private function _createSlide(){
        $file_mark = dirname(__FILE__).'/img/mark2.png';
        $img_mark = imagecreatefrompng($file_mark);
        imagecopy($this->im_slide, $this->im_fullbg,0, $this->_y , $this->_x, $this->_y, self::$mark_width, self::$mark_height);
        imagecopy($this->im_slide, $img_mark,0, $this->_y , 0, 0,self::$mark_width, self::$mark_height);
        imagecolortransparent($this->im_slide,0);//16777215
        //header('Content-Type: image/png');
        //imagepng($this->im_slide);exit;
        imagedestroy($img_mark);
    }

}
?>
